﻿////==============================================================================  
////Copyright (C) 2012-2015 9UN.ORG. All rights reserved. 
////GUID：46172595-6b69-4526-b9da-426d663f5b05
////CLR Version: 4.0.30319.18033
////Code Author：Kevin Wang
////Contact：Email(Admin@9un.org),QQ(265382 or 74344)
////Filename：MediaLibraryFactory
////Namespace：RuanRang.Media.Core
////Functions：MediaLibraryFactory  
////Created by Kevin Wang at 2013/2/2 23:05:09 http://blog.9un.org
////============================================================================== 

//#region Usings

//using System;
//using System.Runtime.InteropServices;
//using RuanRang.Media.Core.Exceptions;
//using RuanRang.Media.Core.Internal.DataStructures;
//using RuanRang.Media.Core.Internal.Interfaces;
//using RuanRang.Media.Core.Internal.Interop;
//using log4net;

//#endregion

//namespace RuanRang.Media.Core.Internal.InternalObjects
//{
//    /// <summary>
//    /// Mapper of original vlc log to specified <seealso cref="ILog"/> logger interface.
//    /// </summary>
//    public sealed class VlcLog : InternalObjectBase
//    {
//        private readonly ILog _logger;

//        /// <summary>
//        /// Pointer to associated libvlc_log_t structure.
//        /// </summary>
//        private readonly IntPtr _descriptor;
//        /// <summary>
//        /// Internal vlclib-managed handle of internal object.
//        /// </summary>
//        /// <value>The descriptor.</value>
//        public override IntPtr Descriptor
//        {
//            get
//            {
//                return (_descriptor);
//            }
//        }

//        /// <summary>
//        /// Pointer to MediaLibraryFactory' descriptor.
//        /// </summary>
//        //private read-only IntPtr ownerDescriptor;
//        private readonly ILogVerbosityManager _logVerbosityManager;

//        /// <summary>
//        /// Initializes a new instance of the <see cref="VlcLog"/> class.
//        /// </summary>
//        /// <param name="descriptor">The descriptor.</param>
//        /// <param name="logVerbosityManager">The log verbosity manager.</param>
//        /// <param name="logger">The logger.</param>
//        /// <exception cref="System.ArgumentException">Zero pointer.;descriptor</exception>
//        /// <exception cref="System.ArgumentNullException">
//        /// logVerbosityManager
//        /// or
//        /// logger
//        /// </exception>
//        public VlcLog(IntPtr descriptor, ILogVerbosityManager logVerbosityManager, ILog logger)
//        {
//            if (descriptor == IntPtr.Zero)
//            {
//                throw new ArgumentException("Zero pointer.", "descriptor");
//            }
//            if (logVerbosityManager == null)
//            {
//                throw new ArgumentNullException("logVerbosityManager");
//            }
//            if (logger == null)
//            {
//                throw new ArgumentNullException("logger");
//            }
//            //
//            _descriptor = descriptor;
//            _logVerbosityManager = logVerbosityManager;
//            _logger = logger;
//        }

//        /// <summary>
//        /// Vlc log verbosity level.
//        /// </summary>
//        /// <value>The verbosity.</value>
//        public int Verbosity
//        {
//            get
//            {
//                VerifyObjectIsNotDisposed();
//                //
//                return (_logVerbosityManager.Verbosity);
//            }
//            set
//            {
//                VerifyObjectIsNotDisposed();
//                //
//                _logVerbosityManager.Verbosity = value;
//            }
//        }

//        /// <summary>
//        /// Updates the messages.
//        /// </summary>
//        /// <exception cref="VlcInternalException">
//        /// </exception>
//        public void UpdateMessages()
//        {
//            VerifyObjectIsNotDisposed();
//            //
//            var iterator = LibVlcLoggingInterop.libvlc_log_get_iterator(_descriptor);
//            if (IntPtr.Zero == iterator)
//            {
//                throw new VlcInternalException(LibVlcCoreInterop.libvlc_errmsg());
//            }
//            //
//            try
//            {
//                while (0 != LibVlcLoggingInterop.libvlc_log_iterator_has_next(iterator))
//                {
//                    //
//                    var messageBuffer = new libvlc_log_message_t
//                                            {
//                                                sizeof_msg =
//                                                    Convert.ToUInt32(
//                                                        Marshal.SizeOf(
//                                                            typeof(libvlc_log_message_t)))
//                                            };
//                    var ptrStructRes = LibVlcLoggingInterop.libvlc_log_iterator_next(iterator, ref messageBuffer);
//                    if (IntPtr.Zero == ptrStructRes)
//                    {
//                        throw new VlcInternalException(LibVlcCoreInterop.libvlc_errmsg());
//                    }
//                    //
//                    var msgRes = (libvlc_log_message_t)Marshal.PtrToStructure(ptrStructRes, typeof(libvlc_log_message_t));
//                    var text = String.Format("{0} {1} {2} : {3}", msgRes.Type, msgRes.Name, msgRes.Header, msgRes.Message);
//                    //
//                    switch (msgRes.Severity)
//                    {
//                        case libvlc_log_message_t_severity.INFO:
//                            {
//                                // INFO
//                                _logger.Info(text);
//                                break;
//                            }
//                        case libvlc_log_message_t_severity.ERR:
//                            {
//                                // ERR
//                                _logger.Error(text);
//                                break;
//                            }
//                        case libvlc_log_message_t_severity.WARN:
//                            {
//                                // WARN
//                                _logger.Warn(text);
//                                break;
//                            }
//                        case libvlc_log_message_t_severity.DBG:
//                            {
//                                // DBG
//                                _logger.Debug(text);
//                                break;
//                            }
//                        default:
//                            {
//                                _logger.Debug("Unknown severity : " + text);
//                                break;
//                            }
//                    }
//                }
//            }
//            finally
//            {
//                LibVlcLoggingInterop.libvlc_log_iterator_free(iterator);
//            }
//        }

//        /// <summary>
//        /// Clears this instance.
//        /// </summary>
//        public void Clear()
//        {
//            VerifyObjectIsNotDisposed();
//            // Clear the vlc log source
//            LibVlcLoggingInterop.libvlc_log_clear(_descriptor);
//        }

//        /// <summary>
//        /// Releases unmanaged and - optionally - managed resources.
//        /// </summary>
//        /// <param name="isDisposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
//        protected override void Dispose(bool isDisposing)
//        {
//            try
//            {
//                // Release unmanaged resources
//                LibVlcLoggingInterop.libvlc_log_close(_descriptor);
//            }
//            finally
//            {
//                base.Dispose(isDisposing);
//            }
//        }
//    }
//}